import { observable, action } from 'mobx';
import * as Apis from '../../apis/Api';

export default class ApiStore {
    @observable apiId = '';// 接口id
    @observable baseList;// 基本信息
    @observable tableList;// 列表
    @observable showTemplet;// 显示模板
    @observable isSuccess = false;// 是否成功数据
    @observable urlQuery;// 拼接完后的参数
    @observable groups = [];// 菜单分组
    @observable parsms = [];// 改接口设置的参数
    @observable singRequestParam = {};// 单接口页面接收到的参数
    @observable subTableList = [];// 子列表
    @observable apiName = '';// 接口名称
    @observable front_path = '';// 前端路径
    @observable apiParamsList = [];// 真正属于该接口的参数及参数值
    @observable updata_date = '';// 数据更新时间
    @observable msgcode = '';// 返回信息
    @observable loading = true;// 加载状态
    @observable isList = '0';// 接口内的数据为纯列表
    @observable thisPage = 1;// 纯列表类接口默认显示第一页，启信宝显示前二十条
    @observable total = 1;// 纯列表类接口一共有多少页
    @observable module = '';
    @observable info = [];
    @observable apiRemark = '';
    @observable apiJson = '';
    @observable frontPath = '';
    @observable disable = false;

    // 多接口页面获取菜单
    @action
    getApiMenu = (combj) => {
      if (combj.props.location) { // 通过链接
        this.requestParam = combj.props.location.query;
        this.module = combj.props.params.module;
        this.params = combj.props.location.query;
      } else { // 通过组件
        this.module = combj.props.module;
        this.requestParam = combj.props.requestPara;
        this.params = combj.props.requestPara;
      }
      if (this.requestParam.EtpFullName) {
        this.name = this.requestParam.EtpFullName;
        // console.log(props);
        Apis.getApi({ apiId: '3e0c6c21300f4825a21679754e84f890', query: JSON.stringify(this.requestParam), isList: '0' }).then((data) => {
          // console.log("第一次请求工商信息！");
          if (data.status != 'success') { // 根据现有参数获取的工商你那个信息为空，处理企业名称并重新请求
            this.GetEnterpriseSubName();
            // console.log("第一次请求失败")
          } else {
            this.getMenu();
          }
        });
      } else {
        this.getMenu();
      }
    }
    /**
 * 获取分组信息
 */
    @action
    getMenu = () => {
      Apis.getMenu({ module: this.module }).then((data) => {
        this.loading = true;
        if (data.status == 'success') {
          this.groups = data.data;
          this.frontPath = data.default_path;
          this.isSuccess = true;
        } else {
          this.msgCode = data.msgcode;
        }
        this.loading = false;
      });
    }
    @action
    GetEnterpriseSubName = () => {
      Apis.getEnterpriseSubName({ name: this.name }).then((data) => {
        this.requestParam.EtpFullName = data.data;
        this.requestParam.keyword = data.data;
        // console.log("处理后的数据："+data.data)
        Apis.getApi({ apiId: '3e0c6c21300f4825a21679754e84f890', query: JSON.stringify(this.requestParam), isList: '0' }).then((data) => {
          // console.log("第二次请求工商信息，处理完企业名称后如果还未获取到工商信息，则跳转到匹配列表中，页面中菜单点击失效。");
          // console.log(data);
          if (data.status == 'success') {
            this.getMenu();
          } else {
            // console.log("第二次请求失败")
            this.disable = true;
            this.loading = false;
          }
        });
      });
    }
    // 多接口页面切换菜单时
    @action
    changeMenu = (key) => {
      this.clrarCatch();
      this.thisPage = 1;
      this.total = 1;
      this.frontPath = key;
    }
    // 获取子列表
    @action
    getSubTable = (subTable, columnName) => {
      this.subTable = subTable;
      this.columnName = columnName;
      this.visible = true;
    }

    // 清除缓存
    @action
    clrarCatch = () => {
      this.apiParamsList = [];
      this.msgcode = '';
      this.isList = '0';
      this.updata_date = '';
      this.isSuccess = false;
      this.loading = true;
    }
}
